package vip.web3.admin.sys.dao.biz;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.mapstruct.Mapping;
import org.springframework.util.CollectionUtils;
import vip.web3.admin.sys.entity.biz.AUserWalletEntity;
import vip.web3.admin.sys.form.user.UserWalletPageForm;
import vip.web3.admin.sys.vo.biz.BizUserWalletVo;
import vip.web3.admin.sys.vo.biz.UserWalletVo;
import vip.web3.admin.sys.vo.biz.export.ExWalletVo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 用户钱包Mapper接口
 *
 * aut-horlilei
 * @date 2024-09-27
 */
@Mapper
public interface AUserWalletDao extends BaseMapper<AUserWalletEntity> {


    /**
     * 批量删除用户钱包
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    int deleteAUserWalletByIds(Integer[] ids);

    int updateAmountCny(@Param("id") Integer id,
                         @Param("amount") BigDecimal amount,
                         @Param("version") Integer version);

    int updateAmountUsdt(@Param("id") Integer id,
                        @Param("amount") BigDecimal amount,
                        @Param("version") Integer version);

    int updateWithdrawCny(@Param("id") Integer id,
                         @Param("amount") BigDecimal amount,
                         @Param("version") Integer version);

    int updateWithdrawUsdt(@Param("id") Integer id,
                         @Param("amount") BigDecimal amount,
                         @Param("version") Integer version);

    int updateMixCny(@Param("id") Integer id,
                      @Param("amount") BigDecimal amount,
                      @Param("withdraw") BigDecimal withdraw,
                      @Param("version") Integer version);

    int updateMixUsdt(@Param("id") Integer id,
                      @Param("amount") BigDecimal amount,
                      @Param("withdraw") BigDecimal withdraw,
                      @Param("version") Integer version);

    IPage<UserWalletVo> pageWallets(IPage<UserWalletVo> page, Map<String, Object> params, UserWalletPageForm form);
    List<ExWalletVo> exWallets(Map<String, Object> params, UserWalletPageForm form);
    default List<BizUserWalletVo> toAdminBizUserWalletVoList(List<UserWalletVo> vos) {
        if (CollectionUtils.isEmpty(vos)) {
            return null;
        }

        List<BizUserWalletVo> list = new ArrayList<>(vos.size());
        for (UserWalletVo vo : vos) {
            list.add(this.toAdminBizUserWalletVo(vo));
        }

        return list;
    }
    default BizUserWalletVo toAdminBizUserWalletVo(UserWalletVo vo) {
        BizUserWalletVo walletVo = toBizUserWalletVo(vo);
        if (Objects.nonNull(walletVo) && Objects.nonNull(vo)) {
            BigDecimal amountCny = Objects.isNull(vo.getAmountCny()) ? BigDecimal.ZERO : vo.getAmountCny();
            BigDecimal amountUsdt = Objects.isNull(vo.getAmountUsdt()) ? BigDecimal.ZERO : vo.getAmountUsdt();
            BigDecimal withdrawCny = Objects.isNull(vo.getWithdrawCny()) ? BigDecimal.ZERO : vo.getWithdrawCny();
            BigDecimal withdrawUsdt = Objects.isNull(vo.getWithdrawUsdt()) ? BigDecimal.ZERO : vo.getWithdrawUsdt();

            walletVo.setAmountCny(amountCny.stripTrailingZeros().toPlainString());
            walletVo.setAmountUsdt(amountUsdt.stripTrailingZeros().toPlainString());
            walletVo.setWithdrawCny(withdrawCny.stripTrailingZeros().toPlainString());
            walletVo.setWithdrawUsdt(withdrawUsdt.stripTrailingZeros().toPlainString());
        }
        return walletVo;
    }

    @Mapping(target = "amountCny", ignore = true)
    @Mapping(target = "amountUsdt", ignore = true)
    @Mapping(target = "withdrawCny", ignore = true)
    @Mapping(target = "withdrawUsdt", ignore = true)
    BizUserWalletVo toBizUserWalletVo(UserWalletVo vo);
}
